package com.nuoniu.sibanyun.mapper.finance;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.finance.VAccountingVoucherMain;
import com.nuoniu.sibanyun.entity.inOut.VInOutRecordMain;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会计凭证主表 Mapper 接口
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-09
 */
public interface VAccountingVoucherMainMapper extends BaseMapper<VAccountingVoucherMain> {

    /**
     * 获取上条数据
     * @author 小啊刚
     * @date 2021-04-14
     */
    @Select("SELECT * FROM fag_voucher WHERE company_id = #{companyId} AND account_id = #{currentAccountId} AND id < #{currentId} ORDER BY id DESC LIMIT 1")
    VAccountingVoucherMain getPreId(Integer companyId, Integer currentAccountId, Long currentId);

    /**
     * 获取下条数据
     * @author 小啊刚
     * @date 2021-04-14
     */
    @Select("SELECT * FROM fag_voucher WHERE company_id = #{companyId} AND account_id = #{currentAccountId} AND id > #{currentId} ORDER BY id ASC LIMIT 1")
    VAccountingVoucherMain getNextId(Integer companyId, Integer currentAccountId, Long currentId);

    /**
     * 获取第一条数据
     * @author 小啊刚
     * @date 2021-04-14
     */
    @Select("SELECT * FROM fag_voucher WHERE company_id = #{companyId} AND account_id = #{currentAccountId} ORDER BY id ASC LIMIT 1")
    VAccountingVoucherMain getFirstId(Integer companyId, Integer currentAccountId);

    /**
     * 获取最后一条数据
     * @author 小啊刚
     * @date 2021-04-14
     */
    @Select("SELECT * FROM fag_voucher WHERE company_id = #{companyId} AND account_id = #{currentAccountId} ORDER BY id desc LIMIT 1")
    VAccountingVoucherMain getLastId(Integer companyId, Integer currentAccountId);

    /**
     * 根据编号获取
     * @author 小啊刚
     * @date 2021-04-15
     */
    @Select("select * from fag_voucher where company_id=#{companyId} and code=#{code}")
    VAccountingVoucherMain getByCode(String code, Integer companyId);

    /**
     * 获取最新一条
     * @author 小啊刚
     * @date 2021-04-16
     */
    @Select("select number from fag_voucher where account_id=#{accountId} and DATE_FORMAT(business_date, '%Y-%m')=DATE_FORMAT(#{businessDate}, '%Y-%m') order by id desc limit 1")
    String getNewest(Integer accountId, LocalDate businessDate);

    @Select("select count(1) from fag_voucher where account_year=#{year} and account_period=#{month} and company_id=#{companyId} and status != 3")
    Integer getBookCount(Integer year, Integer month, Integer companyId);
//zy新增
    @Select("select * from scw_delivery_entry where voucher_date >= #{beginDate} and voucher_date<= #{endDate} and company_id = #{companyId} and voucher_state != 2")
    List<VInOutRecordMain> getBusinessCount(LocalDate beginDate, LocalDate endDate, Integer companyId);


    @Select("select count(1) from fag_voucher where account_year=#{accountYear} and company_id=#{companyId}")
    Integer getBookCountByYear(Integer companyId, Integer accountYear);

    @Update("update fag_voucher set status=0, reviewer_date=null, reviewer_user_id=null, reviewer_user_name=null where id=#{id}")
    void updateAccountingStatus(Long id);

    /**
     * 校验编码是否存在
     * @author 小啊刚
     * @date 2022-06-11
     */
    @Select("select * from fag_voucher where company_id=#{companyId} and account_year=#{currentYear} and account_period=#{currentPeriod} and (code=#{code} or number=#{code}) limit 1 ")
    VAccountingVoucherMain checkCode(Integer companyId, Integer currentYear, Integer currentPeriod, String code);

    /**
     * 获取当前期间已记账最大编号
     * @author 小啊刚
     * @date 2022-06-11
     */
    @Select("select number from fag_voucher where company_id=#{companyId} and account_year=#{accountYear} and account_period=#{accountPeriod} and status=3 order by number desc limit 1 ")
    Integer getBookMaxCode(Integer companyId, Integer accountYear, Integer accountPeriod);

    /**
     * 获取上一个记账编号
     * @author 小啊刚
     * @date 2022-06-13
     */
    @Select("select number from fag_voucher where company_id=#{companyId} and account_year=#{accountYear} and account_period=#{accountPeriod} and number < #{number} and status=3 order by number limit 1 ")
    Integer getBookMinNumber(VAccountingVoucherMain accountingVoucherMain);

    /**
     * 获取连续编号
     * @author 小啊刚
     * @date 2022-06-13
     */
    @Select("select number from fag_voucher where company_id=#{m.companyId} and account_year=#{m.accountYear} and account_period=#{m.accountPeriod} and number < #{m.number} and number > #{minNumber} ")
    Integer[] getBookNumber(Integer minNumber, @Param("m") VAccountingVoucherMain accountingVoucherMain);

    /**
     * 获取当前期间所有凭证号
     * @author 小啊刚
     * @date 2022-06-13
     */
    @Select("select id, code, number from fag_voucher where company_id=#{companyId} and account_year=#{accountYear} and account_period=#{accountPeriod} and number > #{maxNumber} order by number")
    List<VAccountingVoucherMain> getListByPeriod(Integer accountPeriod, Integer accountYear, Integer companyId, Integer maxNumber);
}
